package com.zj.fx.controller.rpa;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.zj.fx.common.base.BaseController;
import com.zj.fx.model.rpa.*;
import com.zj.fx.service.rpa.RpaFetchreliabilityService;
import com.zj.fx.service.rpa.RpaOriginaldataService;
import com.zj.fx.service.rpa.RpaRecordPushService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.net.URLDecoder;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/rpa")
@RequiredArgsConstructor
public class RPAController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(RPAController.class);

    @Autowired
    private RpaFetchreliabilityService rpaFetchreliabilityService;

    @Autowired
    private RpaRecordPushService rpaRecordPushService;

    @Autowired
    private RpaOriginaldataService rpaOriginaldataService;

    /**
     * 供电可靠率数据RPA推送接收接口
     */
    @PostMapping("fetchReliability")
    public void fetchReliability(String data, String type) {
        int count = 0;
        String taskmonth = getYearandMonth();
        String subcompanyName = getSubcompanyName(type);
        //记录调用记录
        RpaRecordPush rpaRecordPush = new RpaRecordPush();
        rpaRecordPush.setTaskname(taskmonth+subcompanyName+"供电可靠率同步");
        rpaRecordPush.setStarttime(new DateTime());
        rpaRecordPush.setDataparam1("data:"+data);
        rpaRecordPush.setDataparam2("type:"+type);

        //20231130 做去重处理，RPA任务可能重复执行，每次插入前查询当前月份分公司是否有记录，有的话将该月份该分公司下所有数据先全部删除
        RpaOriginaldataExample rpaOriginaldataExample = new RpaOriginaldataExample();
        rpaOriginaldataExample.createCriteria().andTaskmonthEqualTo(taskmonth).andSubcompanynameEqualTo(subcompanyName);
        rpaOriginaldataService.deleteByExample(rpaOriginaldataExample);

        String array[][] = JSON.parseObject(URLDecoder.decode(data), String[][].class);
        for (int i = 0; i < array.length; i++) {
//            RpaFetchreliability rpaFetchreliability = new RpaFetchreliability();
//            rpaFetchreliability.setId(array[i][0]);
//            rpaFetchreliability.setSupplyname(array[i][1]);
//            rpaFetchreliability.setRate(array[i][7]);
//            rpaFetchreliability.setTaskmonth(taskmonth);
//            rpaFetchreliability.setSubcompanyname(subcompanyName);
//            rpaFetchreliabilityService.insertSelective(rpaFetchreliability);

            //改为写入到RPA数据通用表
            RpaOriginaldata rpaOriginaldata = new RpaOriginaldata();
            rpaOriginaldata.setTypenum(RpaConstant.TYPE_GONGDIAN_NUM);
            rpaOriginaldata.setTypename(RpaConstant.TYPE_GONGDIAN_NAME);
            rpaOriginaldata.setThirdid(array[i][0]);
            rpaOriginaldata.setSupplyname(array[i][1]);
            rpaOriginaldata.setSubcompanyname(subcompanyName);
            rpaOriginaldata.setTaskmonth(taskmonth);
            rpaOriginaldata.setIsmatch(RpaConstant.IS);
            rpaOriginaldata.setCreatetime(new Date());
            rpaOriginaldata.setIscombine(RpaConstant.NOT);
            rpaOriginaldata.setDatamemo1("供电可靠率");
            rpaOriginaldata.setValue1(array[i][7]);

            if ("国网彰武县供电公司".equals(subcompanyName)&&"福兴地供电所".equals(array[i][1])){
                //20231130 特异化处理，有两个福兴地供电所
                rpaOriginaldata.setSupplyname("重复"+array[i][1]);
            }
            rpaOriginaldataService.insertSelective(rpaOriginaldata);
            count++;
        }

        rpaRecordPush.setEndtime(new DateTime());
        rpaRecordPush.setStatus("1");
        rpaRecordPush.setCount(String.valueOf(count));
        rpaRecordPushService.insertSelective(rpaRecordPush);
    }

    private String getSubcompanyName(String type) {
        String name = "";
        if ("zhaoy1_fx".equals(type)){
            name = "国网阜新蒙古族自治县供电公司";
        } else if ("lj1_fx".equals(type)){
            name = "国网彰武县供电公司";
        } else if ("zly1_fx".equals(type)){
            name = "国网阜新市清河门区供电公司";
        } else if ("ljy4_fx".equals(type)){
            name = "国网阜新市细河区供电公司";
        } else if ("xiangp_fx".equals(type)){
            name = "国网阜新市市南供电公司";
        }
        return name;
    }

    private String getYearandMonth() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前年份
        int year = currentDate.getYear();
        // 获取当前月份
        int month = currentDate.getMonthValue();
        String smonth = "" + month;
        if (month < 10) {
            smonth = "0" + month;
        }
        String taskmonth = year + smonth;
        return taskmonth;
    }
}
